<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

	<p:param type="input" name="instance"/>

	<p:processor name="oxf:pipeline">
		<p:input name="config" href="../session.xpl"/>
		<p:output name="data" id="session"/>
	</p:processor>

	<p:processor name="oxf:xslt">
		<p:input name="data" href="#instance"/>
		<p:output name="data" id="file"/>
		<p:input name="config">
			<config xsl:version="2.0">
				<url>/widgets/<xsl:value-of select="/submission/widgetId"/>/config.xml</url>
				<content-type>application/xml</content-type>
				<validating>true</validating>
				<handle-xinclude>false</handle-xinclude>
			</config>
		</p:input>
	</p:processor>
	<p:processor name="oxf:url-generator">
		<p:input name="config" href="#file"/>
		<p:output name="data" id="original_manifest"/>
	</p:processor>

	<p:processor name="oxf:xslt">
		<p:input name="data" href="#instance"/>
		<p:input name="session" href="#session"/>
		<p:input name="config">
			<xdb:query xsl:version="2.0" collection="/db/portal">
				<xsl:text>xquery version "1.0";</xsl:text>
				<preferences><xsl:choose>
						<xsl:when test="doc('input:session')/session/groups/@current = 'gr_0'">{/users/user[@id='<xsl:value-of select="doc('input:session')/session/username"/>']/interface/widget[@id = '<xsl:value-of select="/submission/widgetId"/>']/preferences/*}</xsl:when>
						<xsl:otherwise>
						<xsl:variable name="gr" select="doc('input:session')/session/groups/@current"/>
						<xsl:variable name="wgt" select="/submission/widgetId"/>
							<xsl:copy-of select="doc('input:session')/session/groups/group[@id=$gr]/interface/widget[@id = $wgt]/preferences/*"/>
						</xsl:otherwise>
					</xsl:choose>
				</preferences>
			</xdb:query>
		</p:input>
		<p:output name="data" id="query"/>
	</p:processor>

	<p:processor name="oxf:xmldb-query">
		<p:input name="datasource" href="/config/datasource.xml"/>
		<p:input name="query" href="#query"/>
		<p:output name="data" id="preferences"/>
	</p:processor>

	<p:processor name="oxf:xslt">
		<p:input name="data" href="aggregate('response', aggregate('config',#original_manifest), #preferences)"/>
		<p:input name="config">
			<xsl:stylesheet version="2.0" xmlns:palette="http://palette.ercim.org/ns/">
				<xsl:template match="/">
					<xsl:apply-templates select="/response/config/*"/>
				</xsl:template>
				<xsl:template match="palette:preference">
					<xsl:variable name="name" select="./@name"/>
					<xsl:copy>
						<xsl:attribute name="value">
							<xsl:choose>
								<xsl:when test="/response/preferences/*[name(.) = $name]">
									<xsl:value-of select="/response/preferences/*[name(.) = $name]"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="./@default_value"/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:attribute>
						<xsl:apply-templates select="@*"/>
					</xsl:copy>
				</xsl:template>

				<xsl:template match="*">
					<xsl:copy>
						<xsl:apply-templates select="@*| *"/>
						<xsl:value-of select="."/>
					</xsl:copy>
				</xsl:template>

				<xsl:template match="@*">
					<xsl:copy-of select="."/>
				</xsl:template>
			</xsl:stylesheet>
		</p:input>
		<p:output name="data" id="modified_manifest"/>
	</p:processor>

	<p:processor name="oxf:xml-converter">
		<p:input name="config">
			<config>
				<content-type>text/xml</content-type>
				<encoding>utf-8</encoding>
				<version>1.0</version>
				<omit-xml-declaration>true</omit-xml-declaration>
			</config>
		</p:input>
		<p:input name="data" href="#modified_manifest"/>
		<p:output name="data" id="xml-doc"/>
	</p:processor>

	<p:processor name="oxf:http-serializer">
		<p:input name="config">
			<config>
				<content-type>text/xml</content-type>
				<force-content-type>true</force-content-type>
			</config>
		</p:input>
		<p:input name="data" href="#xml-doc"/>
	</p:processor>

</p:config>